package Listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class OnlineListener implements HttpSessionListener {
    private int count = 0;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        count++;
        ServletContext context = event.getSession().getServletContext();
        context.setAttribute("count", count);  // 更新在线人数
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        count--;
        ServletContext context = event.getSession().getServletContext();
        context.setAttribute("count", count);  // 更新在线人数
    }
}
